package org.iabako.shared.querydefinition.dto;

import org.iabako.shared.querydefinition.columndefinition.ColumnDefinition;
import org.iabako.shared.tools.GenericTools;

import java.io.Serializable;
import java.util.Date;

/**
 * TODO javadoc
 * Date: 24/11/13
 * Time: 18:22
 */
public class CriteriaFilterDTO implements Serializable {

    private ColumnDefinition key;
    private Serializable value;
    private boolean dateValue;
    private QueryOperatorEnum queryOperator;
    private boolean transientCriteria;

    public CriteriaFilterDTO() {
    }

    public CriteriaFilterDTO(ColumnDefinition key, Serializable value, QueryOperatorEnum queryOperator) {
        this.key = key;
        if (value instanceof Date) {
            this.value = GenericTools.formatDateToString((Date) value);
            this.dateValue = true;
        } else {
            this.value = value;
        }
        this.queryOperator = queryOperator;
    }

    public ColumnDefinition getKey() {
        return key;
    }

    public Serializable getValue() {
        if (dateValue && value instanceof String) {
            value = GenericTools.getDateFromString((String) value);
        }
        if (queryOperator == QueryOperatorEnum.LIKE) {
            return "%" + value + "%";
        }
        return value;
    }

    public QueryOperatorEnum getQueryOperator() {
        return queryOperator;
    }

    public boolean isTransientCriteria() {
        return transientCriteria;
    }

    public void setTransientCriteria(boolean transientCriteria) {
        this.transientCriteria = transientCriteria;
    }
}
